package com.future.mayi.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.future.mayi.app.App;
import com.future.mayi.utils.HCUtils;


public class ViewHolder {

    private final SparseArray<View> mViews;
    private int mPosition;

    private Context mContext;

    private View mConvertView;

    private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mContext = context;
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder(context, parent, layoutId, position);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.mPosition = position;
        }
        return holder;
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public TextView setText(int viewId, String text) {
        TextView view = getView(viewId);
        if (TextUtils.isEmpty(text))
            view.setText("");
        else
            view.setText(text);
        return view;
    }


    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, int text) {
        TextView view = getView(viewId);

        view.setText(String.valueOf(text));
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 加载网络图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public ImageView setWebImage(int viewId, String url) {
        ImageView imageView = getView(viewId);
        HCUtils.loadWebImg(App.getmContext(),imageView, url);
        return imageView;
    }

    /**
     * 加载网络图片
     *
     * @param imageView
     * @param url
     * @return
     */
    public ViewHolder setWebImage(ImageView imageView, String url) {
        HCUtils.loadWebImg(App.getmContext(),imageView, url);
        return this;
    }


    /**
     * 设置visibility
     *
     * @param viewId
     * @param visibility
     * @return
     */
    public void setVisibility(int viewId, int visibility) {
        getView(viewId).setVisibility(visibility);
    }


    public int getPosition() {
        return mPosition;
    }

    public ViewHolder setListView(int viewid, BaseAdapter adapter) {
        ListView listView = getView(viewid);
        listView.setAdapter(adapter);
        return this;
    }


}
